<?php namespace App\Models;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use App\Models\Property;

/**
 * App\Models\Rater
 *
 * @property integer $id
 * @property string $name 名称
 * @property string $username 用户名
 * @property string $email email
 * @property string $password 密码
 * @property string $remember_token
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Rater whereId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Rater whereName($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Rater whereUsername($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Rater whereEmail($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Rater wherePassword($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Rater whereRememberToken($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Rater whereCreatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Rater whereUpdatedAt($value)
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Vote[] $votes
 * @mixin \Eloquent
 */
class Rater extends Model implements AuthenticatableContract,
    AuthorizableContract,
    CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'raters';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'username',
        'email',
        'zebian',
        'category',
        'province_id',
        'city_id',
        'ems',
        'job',
        'mobile',
        'work_unit',
        'personal_introduction'

    ];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];

    public function votes()
    {
        return $this->belongsToMany(Vote::class, 'vote_rater', 'rater_id', 'vote_id')
            ->withPivot(['poll_max', 'poll_min', 'confirmed']);
    }

    /**
     * 获取分类名称
     */
    public function getCategoryName()
    {
        return Property::getValueByIndex('评委分类',$this->category);
    }

    public function honors()
    {
        return $this->belongsToMany( '\App\Models\Honor' , 'honor_rate_teacher', 'rate_id', 'honor_id');
    }

    public function getHonorIds()
    {
        $honorIds = $this->honors->lists('id')->toArray();
        return $honorIds;
    }
}
